<?php
require_once 'BaseController.php';

/**
 * 物件お問合せ完了画面コントローラクラス<br />
 *
 * @package apamanshop
 * @author A.Miyata
 * @copyright　
 */
class Inquiry_BukkensendController extends Inquiry_BaseController
{
	/**
	 * インデックスアクション
	 *
	 */
	public function indexAction(){

		//表示テンプレートパス
		$this->_display_path = APPLICATION_PATH.'/modules/inquiry/views/scripts/estate.tpl';

		// パラメータの取得
		//$this->_params = $this->getParameters();
		$this->_params['TimeStr'] = date("Y/m/d H:i:s");

		// こだわりデータ
		if($this->_params['kodawariData'] !=""){
			for($i = 0; $i <= mb_strlen($this->_params['kodawariData']);$i++){
				$this->_params['kodawari' . ($i+1)] = substr($this->_params['kodawariData'],$i,1);
			}
		}

		// キャンペーン設定（JavaScriptでは上手く動かないので、ここでセット）
		$CampaignAgechau = "";
		foreach($this->_params as $key => $val) {
			if (strpos($key, 'CampaignFlag_') !== false) {
				if ($CampaignAgechau != "") $CampaignAgechau .= ";";
				$CampaignAgechau .= str_replace('CampaignFlag_', '', $key);
			}
		}
		$this->_params['CampaignAgechau'] = $CampaignAgechau;

		// 災害時住宅支援対応
		if (!empty($this->_params['sienJutaku']) && $this->_params['sienJutaku'] == "on") {
			$this->_params['vintGenruFlg'] = '75';
		}

		//問い合わせAPI実行
	    $apiCtlName ='apibukken';
		$this->callInquiryApi($apiCtlName,$this->_params);

		// リロード先へデータセット
		$re_params = array();
		$re_params['HeyaCd'] = $this->_params['BukkenCd'];
		$re_params['UseKbn'] = $this->_params['UseKbn'];
		$re_params['OemCd']  = $this->_params['OemCd'];
		$re_params['CV_ToiawaseId']   = $this->_CV_data['CV_ToiawaseId'];
		$re_params['CV_TotalAmounts'] = $this->_CV_data['CV_TotalAmounts'];
		$re_params['CV_bukken'] = array();
		if (!empty($this->_CV_data['CV_bukken'])) {
			foreach ($this->_CV_data['CV_bukken'] as $key => $val) {
				$value =      $val['T03_ORIGINAL_HEYA_CD01']
						."<>".mb_convert_encoding($val['M05_TENPO_NAME'], 'SJIS', 'UTF-8')
						."<>".mb_convert_encoding($val['M01_TODOUFUKEN'], 'SJIS', 'UTF-8')
						."<>".mb_convert_encoding($val['M01_SHIKUCHOUSON'], 'SJIS', 'UTF-8')
						."<>".mb_convert_encoding($val['T03_MADORI'], 'SJIS', 'UTF-8')
						."<>".$val['T03_YACHIN'];
				$re_params['CV_bukken'][] = $value;
			}
		}
		if (!empty($this->_params['shoppage'])) {
			$re_params['shoppage'] = $this->_params['shoppage'];
			$re_params['ShopCd']   = $this->_params['ShopCd'];
		}

		// セッション開始（キャンペーン用）
		$mySession = new Zend_Session_Namespace('InquiryCampaign');
		$mySession->CampaignAgechau = $CampaignAgechau;
		$mySession->ToiawaseId = $this->_CV_data['CV_ToiawaseId'];

		//F5リロードによる２重送信防止のため完了画面表示にリダイレクト
		Apm_Util::redirect($this->_baseurl.'inquiry/bukkensend/show',$re_params);
		return;

		//=====以下は処理しない=====

	    $loader = new Zend_Application_Module_Autoloader(array(
			'namespace' => 'Rstobi',
			'basePath' => APPLICATION_PATH . '/modules/rstobi',
		));
		$recommend_model = new Rstobi_Model_BknRecommend();
		$this->_params['HeyaCd'] = $this->_params['BukkenCd'];
		$this->_params['SrhKbn'] = "01";

		//=====表示用共通パラメータ設定=====
		$this->setCommonParams();
		$this->setSubParams();
		$this->_data = $this->_params;

		// レコメンド物件情報取得
		$this->_data['recommend'] = $recommend_model->getRecommendList($this->_params);
		// 表示用にコンバート
		$this->_data['recommend'] = $recommend_model->convertRecommendList($this->_data['recommend']);

		// CVデータセット
		$this->_data['cv_data'] = $this->_CV_data;

		//ブロック設定
		$this->setBlockBukkenScript();
		$this->setSubblockHpCommon();
		$this->setBlock($this->_bukkensend_helper);

		//表示
		$this->display($this->_display_path);
	}

	/**
	 * 完了画面表示用アクション
	 */
	public function showAction()
	{
		$this->setMainTemplate('cms_search2.tpl');
		//表示テンプレートパス
		//$this->_display_path = APPLICATION_PATH.'/modules/inquiry/views/scripts/estate.tpl';
		// テンプレート設定
		// OEM判定 法人判定
		if($this->isOem($this->_params)){
			// ヘッダ・フッター非表示
    		$this->setXPassThroutgh();
    		// メインテンプレート
    		//$disp_tpl = APPLICATION_PATH . '/modules/inquiry/views/scripts/nohead.tpl';
    		$this->_display_path = APPLICATION_PATH . '/modules/inquiry/views/scripts/nohead.tpl';
		}else{
			//表示テンプレートパス
			$this->_display_path = APPLICATION_PATH.'/modules/inquiry/views/scripts/estate.tpl';
		}

		// パラメータの取得
		//$this->_params = $this->getParameters();

	    $loader = new Zend_Application_Module_Autoloader(array(
			'namespace' => 'Rstobi',
			'basePath' => APPLICATION_PATH . '/modules/rstobi',
		));
		$recommend_model = new Rstobi_Model_BknRecommend();
		//$this->_params['HeyaCd'] = $this->_params['BukkenCd'];
		$this->_params['SrhKbn'] = "01";

		//=====表示用共通パラメータ設定=====
		$this->setCommonParams();
		$this->setSubParams();
		$this->_data = $this->_params;

		// レコメンド物件情報取得
		if (!empty($this->_params['HeyaCd'])) {
			// 部屋CDが複数の場合,先頭のみを対象とする。
			$heyacd = $this->_params['HeyaCd'];
			$temp = explode(",", $heyacd);
			$this->_params['HeyaCd'] = $temp[0];

			$this->_data['recommend'] = $recommend_model->getRecommendList($this->_params);
			// 表示用にコンバート
			$this->_data['recommend'] = $recommend_model->convertRecommendList($this->_data['recommend']);

			// 部屋CDを戻す
			$this->_params['HeyaCd'] = $heyacd;
		} else {
			// 物件CDが無かった時対応（テスト用）
			$this->_data['recommend'] = array();
		}

		// バナーエリア追加(2010/12/22)
		$this->_publicPath = $this->_config['apm']['baseurl'];
		$banner_file = $this->_publicPath.'css/inquiry/hp/Banner.inc';
		try {
			$x = get_headers($banner_file);
			if (preg_match("/OK$/", $x[0]))
			{
			//if ($this->url_exists($banner_file)) {
				$buf = implode("",file($banner_file));
				$this->_data['banner_block'] = $buf;
			}
		} catch (Exception $e) {
		}

		// セッション開始（キャンペーン用）
		$mySession = new Zend_Session_Namespace('InquiryCampaign');
		// キャンペーンCD判定
		$campaign = split(';', $mySession->CampaignAgechau);
		$this->_data['campaign_block'] = "";
		// 総計10万Pontaポイントプレゼント対応
		if (isset($mySession->ToiawaseId) && date('Ymd') <= '20140831') {
			$this->_data['campaign_block'] .= '<p><a href="'.$this->_sslurl.'inquiry/ponta201408/"><img src="/shared/images/ponta201408.jpg" alt="総計10万Pontaポイントプレゼント" /></a></p><br />';
		}
		// サマーデイズキャンペーン壁紙DLリンク
		if (in_array('81', $campaign)) {
			$this->_data['campaign_block'] .= '<p><a href="/campaign/1406_rootfive/download/index.html"><img src="/campaign/1406_rootfive/images/bnr_toiawase.jpg" alt="サマーデイズキャンペーンオリジナル壁紙ダウンロードはこちら！" /></a></p><br />';
		}

		// CVデータセット
		$this->_data['cv_data']['CV_ToiawaseId']   = $this->_params['CV_ToiawaseId'];
		$this->_data['cv_data']['CV_TotalAmounts'] = $this->_params['CV_TotalAmounts'];
		if (!empty($this->_params['CV_bukken'])) {
			foreach ($this->_params['CV_bukken'] as $key => $val) {
				$value = split("<>", $val);
				$this->_data['cv_data']['CV_bukken'][] = array(
						"T03_ORIGINAL_HEYA_CD01" => $value[0],
						"M05_TENPO_NAME" => $value[1],
						"M01_TODOUFUKEN" => $value[2],
						"M01_SHIKUCHOUSON" => $value[3],
						"T03_MADORI" => $value[4],
						"T03_YACHIN" => $value[5]
					);
			}
		}

		//加盟店ページより遷移時はヘッダを付与する Add 2012/05/30 H.Imamura
		if($this->_params['shoppage']){
			// 外部モデル呼び出し設定
			$loader = new Zend_Application_Module_Autoloader(array(
				'namespace' => 'Shop',
				'basePath' => APPLICATION_PATH . '/modules/shop',
			));
			$this->_model_shop = new Shop_Model_Detail();
			// 店舗詳細情報取得
			$shopDtlList = $this->_model_shop->getList($this->_params);
			// 表示用に変換
			$this->_data['shopDtl'] = $this->_model_shop->convertData($shopDtlList[0]);
			//ブロック設定
			$this->setBlockShoppageHeader();
		}

		//ブロック設定
		$this->setBlockBukkenScript();
		$this->setSubblockHpCommon();
		$this->setBlock($this->_bukkensend_helper);

		//表示
		$this->display($this->_display_path);
	}

	/**
	 * 物件お問合せ画面共通パラメータ設定
	 *
	 */
	private function setSubParams()
	{
		//=====タイトル=====
		$this->setTitle('賃貸情報アパマンショップ[物件お問い合わせ]');

		//=====フッタのトップへ戻るコメント=====
		$this->_params['footerTitle'] = '賃貸物件お問い合わせページ';

		//=====タイトルバー設定=====
		$this->_params['contentsTitle'] = '賃貸物件検索　≫　お問い合わせ完了';

		// パンくず設定
		$this->_breadcrumb->add('お問い合わせ ', $this->_sslurl . 'inquiry/bukkenentry');
        $this->setBreadCrumb($this->_breadcrumb->generateBreadcrumb());
	}

	/**
	* OEM判定
	*/
	private function isOem($params)
	{
		return ((array_key_exists('OemCd', $params) == true) && (is_null($params['OemCd']) == false) && (is_numeric($params['OemCd']) == true));
	}

	/**
	* ファイル存在チェック
	*/
	function url_exists($url) {
		// Version 4.x supported
		$handle   = curl_init($url);
		if (false === $handle)
		{
			return false;
		}
		curl_setopt($handle, CURLOPT_HEADER, false);
		curl_setopt($handle, CURLOPT_FAILONERROR, true);  // this works
		curl_setopt($handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox   
		curl_setopt($handle, CURLOPT_NOBODY, true);
		curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
		$connectable = curl_exec($handle);
		curl_close($handle);  
		return $connectable;
	}

}
?>